﻿var zombies = zombies || {};

zombies.ListCategory = function () {
    var self = this;
    self.Categories = ko.observableArray();

    self.SearchString = ko.observable("");

    self.SendMsg = function (data, event) {
        try {
            if (event.which === 13) {
                toastr.info(self.SearchString());
                return false;
            }
            return true;
        }
        catch (e)
        { }
        
    };

    self.CatId = ko.observable();
    self.Name = ko.observable();
    self.Description = ko.observable();
    self.IconPath = ko.observable();

    self.CategoryData = ko.observable({
        CatId: self.CatId,
        Name: self.Name,
        Description: self.Description,
        IconPath: self.IconPath
    });

    self.old = ko.observable();

    self.getEditItem = function (item) {
        $('#editCategory').css('display', 'block');
        $('#modal-div').css('display', 'block');
        self.CatId(item.CatId);
        self.Name(item.Name);
        self.Description(item.Description);
        self.IconPath(item.IconPath);
    }

    self.doEdit = function (formElement) {
        var category = ko.utils.arrayFirst(self.Categories(), function (item) {
            return item.CatId === self.CatId();
        });
        if ($(formElement).valid()) {
            $.ajax({
                type: "PUT",
                url: "/api/AdminApi/PutCategory",
                data: ko.toJSON(self.CategoryData), //Convert the Observable Data into JSON
                contentType: "application/json",
                dataType: 'json'
            }).done(function () {
                toastr.success('Edit Category Successfully !');
                self.Categories.replace(category, { CatId: self.CatId(), Name: self.Name(), Description: self.Description(), IconPath: self.IconPath()});
                $('#editCategory').css('display', 'none');
            }).fail(function () {
                toastr.error('Edit Category Failed !');
            });
        }
    }

    self.doDelete = function (item) {
        $.ajax({
            type: "DELETE",
            url: "/api/AdminApi/DeleteCategory/" + item.CatId,
            success: function (data) {
                self.Categories.remove(item);
                toastr.success("Delete Category Successfully !");
            },
            error: function (error) {
                toastr.error("Delete Category Failed !");
            }
        });
    }
}

var load = function () {
    $.get("/api/AdminApi/GetCategories", function (items) {
        $.each(items, function (idx, item) {
            viewModel.Categories.push(new zombies.Category(item));
        });
    }, "json").done(function () { toastr.success("Load Category Successfully !") });
};

var viewModel;
$(document).ready(function () {

    viewModel = new zombies.ListCategory();

    ko.applyBindings(viewModel);

    load();
});